package com.easylinkin.linkappapi.analyze.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.analyze.entity.AnalyzeDateSource;
import com.easylinkin.linkappapi.analyze.entity.DdeviceVo;
import com.easylinkin.linkappapi.analyze.service.AnalyzeDateSourceService;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.space.service.LinkappAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 数据分析源
 * </p>
 *
 * @author XuGuoQing
 * @since 2020-04-23
 */
@RestController
@Api(value = "数据分析源", tags = {"数据分析源"})
@RequestMapping("/analyze/analyzeDateSource")
public class AnalyzeDateSourceController {

    private static final int ON_LINE = 1;
    private static final int OFF_LINE = 0;
    @Resource
    private AnalyzeDateSourceService analyzeDateSourceService;
    @Resource
    private LinkappAreaService linkappAreaService;

    @ApiOperation("新增数据源")
    @CommonOperateLogAnnotate(module = LogModule.DATA_SOURCE, desc = "新增数据源")
    @PostMapping("/addDateSource")
    public RestMessage addDateSource(@RequestBody AnalyzeDateSource dateSource) {
        return RestBuilders.successBuilder().success(analyzeDateSourceService.addAnalyzeDateSource(dateSource)).build();
    }

    @ApiOperation("根据租户id查询数据源 ")
    @PostMapping("/getDateSourceByTeantId")
    public RestMessage getDateSourceByTeantId() {
        return RestBuilders.successBuilder().data(analyzeDateSourceService.getDateSourceList()).build();
    }

    @ApiOperation("删除数据源")
    @CommonOperateLogAnnotate(module = LogModule.DATA_SOURCE, desc = "删除数据源")
    @PostMapping("/deleteDateSource")
    public RestMessage deleteDateSource(@RequestBody List<String> dateSourceIdlist) {
        return RestBuilders.successBuilder().success(analyzeDateSourceService.deleteAnalyzeDateSource(dateSourceIdlist)).build();
    }

    @ApiOperation("编辑数据源")
    @CommonOperateLogAnnotate(module = LogModule.DATA_SOURCE, desc = "编辑数据源")
    @PostMapping("/updateDateSource")
    public RestMessage updateLinkappAsset(@RequestBody AnalyzeDateSource analyzeDateSource) {
        return RestBuilders.successBuilder().success(analyzeDateSourceService.updateAnalyzeDateSource(analyzeDateSource)).build();
    }

    @ApiOperation("查询某个数据源")
    @PostMapping("/getDateSourceById")
    public RestMessage getDateSourceById(@RequestBody AnalyzeDateSource analyzeDateSource) {
        return RestBuilders.successBuilder().data(analyzeDateSourceService.getDateSourceById(analyzeDateSource)).build();
    }

    @ApiOperation("数据源列表")
    @PostMapping("/getDateSourcePage")
    public RestMessage getDateSourcePage(@RequestBody RequestModel<AnalyzeDateSource> requestModel) {
        IPage<AnalyzeDateSource> record = analyzeDateSourceService.getPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    @ApiOperation("数据源按条件查询所有")
    @PostMapping("/getDateSources")
    public RestMessage getDateSources(@RequestBody AnalyzeDateSource requestModel) {
        return RestBuilders.successBuilder().data(analyzeDateSourceService.getDateSources(requestModel)).build();
    }

    @ApiOperation("查询当数据源没有绑定的设备")
    @PostMapping("/getDevicePage")
    public RestMessage getNoBundlingDevicePage(@RequestBody RequestModel<DdeviceVo> requestModel) {
        IPage<Device> record = analyzeDateSourceService.getDeviceByDataSourceId(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    @ApiOperation("给数据源添加设备")
    @CommonOperateLogAnnotate(module = LogModule.DATA_SOURCE, desc = "给数据源添加设备")
    @PostMapping("/addDeviceToDateSource")
    public RestMessage addDeviceToDateSource(@RequestBody AnalyzeDateSource analyzeDateSource) {
        return RestBuilders.successBuilder().success(analyzeDateSourceService.addDeviceToDateSource(analyzeDateSource)).build();
    }

    @ApiOperation("给数据源移除设备")
    @CommonOperateLogAnnotate(module = LogModule.DATA_SOURCE, desc = "给数据源移除设备")
    @PostMapping("/removeDeviceFromDateSource")
    public RestMessage removeDeviceFromDateSource(@RequestBody AnalyzeDateSource analyzeDateSource) {
        return RestBuilders.successBuilder().success(analyzeDateSourceService.removeDeviceFromDateSource(analyzeDateSource)).build();
    }

    @ApiOperation("数据源启/停用")
    @CommonOperateLogAnnotate(module = LogModule.DATA_SOURCE, desc = "数据源启停")
    @PostMapping("/updateAnalyzeDateStatus")
    public RestMessage updateAnalyzeDateStatus(@RequestBody AnalyzeDateSource analyzeDateSource) {
        return RestBuilders.successBuilder().success(analyzeDateSourceService.updateAnalyzeDateStatus(analyzeDateSource)).build();
    }

}
